<template>
  <el-dialog
    v-model="dialogEndable"
    :title="dialogTitle"
  >
    <el-form :model="project" label-width="auto">
      <el-form-item label="项目名称">
        <el-input v-model="project.name" />
      </el-form-item>
      <el-form-item label="当前版本">
        <el-input v-model="project.current_version" />
      </el-form-item>
      <el-form-item label="负责人">
        <el-select v-model="project.leader.id" filterable>
          <el-option v-for="user in users"
          :key="user.id"
          :label="user.name"
          :value="user.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="立项时间">
        <el-date-picker
        v-model="dateValue"
        type="date"
        placeholder="Pick a Date"
        format="YYYY 年 M 月 D 日"
        value-format="YYYY-M-D" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="submitUpdate">
          确认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { post } from '../utils/common/api'

export default {
  data() {
    return {
      dialogEndable: false,

      dialogTitle: '编辑项目',

      id: 1,

      project: {
        name: 'DocMan',

        leader: {
          id: 1
        },

        current_version: '',

        start_time: {
          // year: 2023,
          // month: 9,
          // day: 27
        }
      },

      users: [],

      dateValue: ''
    }
  },

  methods: {
    openDialog() {
      this.dialogEndable = true
    },

    closeDialog() {
      this.dialogEndable = false
    },

    setProject(id, proj) {
      this.id = id
      this.project = JSON.parse(JSON.stringify(proj)),
      this.dialogTitle = `编辑项目 No.${id}`
      var start_time = proj.start_time
      this.dateValue = `${start_time.year}-${start_time.month}-${start_time.day}`
    },

    updateUsers() {
      var that = this
      post('/user/all', {}).then(function(res) {
        that.users = res.list
      })
    },

    submitUpdate() {
      var that = this
      if (that.dateValue != undefined) {
        that.project.start_time = {
          year: parseInt(that.dateValue.split('-')[0]),
          month: parseInt(that.dateValue.split('-')[1]),
          day: parseInt(that.dateValue.split('-')[2])
        }
      }
      console.log(that.project)
      post('/project/update', {
        id: that.id,
        mod: that.project
      }).then(function(res) {
        that.$emit('update')
        ElNotification({
          title: '更新成功',
          message: `您刚刚修改了 ${res.name} 项目的相关信息`,
          type: 'success',
          position: 'bottom-right',
        })
        that.closeDialog()
      })
    }
  },

  mounted() {
    this.updateUsers()
  }
}
</script>